import 'dart:math';

import 'package:auntserver/model/entitys.dart';
import 'package:auntserver/net_api/aunt_datas.dart';
import 'package:auntserver/utils/aunt_utils.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:auntserver/app_constants.dart';
import 'package:auntserver/model/beans.dart';
import 'package:provider/provider.dart';
import 'package:auntserver/router_handler/routers.dart';

import '../application.dart';
/*-------------------选择身份------------------------*/
class ChooseIdentityScreen extends StatefulWidget {
  @override
  _ChooseIdentityScreenState createState() => _ChooseIdentityScreenState();
}

class _ChooseIdentityScreenState extends State<ChooseIdentityScreen> {
  ChooseIdentity _chooseIdentity;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) => _isJump(context));
  }

  @override
  Widget build(BuildContext context) {
    _chooseIdentity = Provider.of<ChooseIdentity>(context);
    return SafeArea(
      child: Scaffold(
        body: Padding(
          padding: EdgeInsets.symmetric(
              horizontal: ScreenUtil.getInstance().getWidth(16)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              SizedBox(
                height: ScreenUtil.getInstance().getHeight(48),
              ),
              Text(
                "欢迎登录表叔云服务端",
                style: TextStyle(fontSize: 24, fontWeight: FontWeight.w500),
              ),
              SizedBox(
                height: ScreenUtil.getInstance().getHeight(16),
              ),
              Text(
                "请选择身份",
                style: TextStyle(fontSize: 18),
              ),
              SizedBox(
                height: ScreenUtil.getInstance().getHeight(90),
              ),
              GestureDetector(
                onTap: (){
                  AuntApp.router.navigateTo(context, Routes.precious,
                      routeSettings: RouteSettings(arguments: "arguments"));
                },
                child: Container(
                  child: Center(
                    child: SizedBox(
                      height: ScreenUtil.getInstance().getHeight(240),
                      child: Image.asset(Utils.getImgPath("p1")),
                    ),
                  ),
                ),
              ),
              SizedBox(
                height: ScreenUtil.getInstance().getHeight(40),
              ),
              Row(
                children: [
                  Expanded(
                    flex: 1,
                    child: GestureDetector(
                      onTap: _chooseCompany,
                      child: Container(
                        height: ScreenUtil.getInstance().getHeight(44),
                        decoration: new BoxDecoration(
                          //背景
                          color: Colors.white,
                          //设置四周圆角 角度
                          borderRadius: BorderRadius.all(Radius.circular(4.0)),
                          //设置四周边框
                          border: new Border.all(
                              width: 1, color: AuntColors.AORANGE),
                        ),
                        child: Center(
                          child: Text(
                            "我有团队",
                            style: TextStyle(color: AuntColors.AORANGE),
                          ),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(
                    height: ScreenUtil.getInstance().getHeight(44),
                    width: ScreenUtil.getInstance().getWidth(8),
                  ),
                  Expanded(
                    flex: 1,
                    child: GestureDetector(
                      onTap: _chooseAunt,
                      child: Container(
                        height: ScreenUtil.getInstance().getHeight(44),
                        decoration: new BoxDecoration(
                          color: AuntColors.AORANGE,
                          borderRadius: BorderRadius.all(Radius.circular(
                              ScreenUtil.getInstance().getWidth(4))),
                        ),
                        child: Center(
                            child: Text(
                          "我要接单",
                          style: TextStyle(color: AuntColors.AWHITE),
                        )),
                      ),
                    ),
                  )
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _chooseAunt() {
    _chooseIdentity.setStatus(1);
    AuntApp.router.navigateTo(context, Routes.inputPhone);
  }

  void _chooseCompany() {
    _chooseIdentity.setStatus(2);
    AuntApp.router.navigateTo(context, Routes.inputPhone);
  }

  void _isJump(BuildContext context) {
    if (_chooseIdentity.identityStatus != IdentityStatus.unselected) {
      AuntApp.router.navigateTo(context, Routes.inputPhone);
    }
  }
}



/*-------------------输入密码------------------------*/

class InputPasswordScreen extends StatefulWidget {
  final String _phone;

  InputPasswordScreen(this._phone);

  @override
  _InputPasswordScreenState createState() => _InputPasswordScreenState();
}

class _InputPasswordScreenState extends State<InputPasswordScreen> {
  TextEditingController _controller;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Map loginInfo = SpUtil.getObject(KeyConstant.key_login);
    _controller = new TextEditingController(text:loginInfo!=null?(loginInfo["password"]??""):"");
  }
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: EdgeInsets.symmetric(
                  horizontal: ScreenUtil.getInstance().getWidth(16)),
              child: Container(
                height: ScreenUtil.getInstance().getHeight(50),
                child: Icon(Icons.arrow_back),
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(
                  horizontal: ScreenUtil.getInstance().getWidth(16)),
              child: Text("请输入密码"),
            ),
            Padding(
              padding: EdgeInsets.symmetric(
                  horizontal: ScreenUtil.getInstance().getWidth(16)),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text("当前账号："+widget._phone),
                  Row(
                    children: <Widget>[
                      Icon(Icons.loop),
                      Text(
                        '切换身份',
                        style: TextStyle(
                          fontSize: 8,
                        ),
                      )
                    ],
                  ),
                ],
              ),
            ),
            SizedBox(
              height: ScreenUtil.getInstance().getHeight(60),
            ),
            Padding(
              padding: EdgeInsets.symmetric(
                  horizontal: ScreenUtil.getInstance().getWidth(36)),
              child: Column(
                children: [
                  Row(
                    children: [
                      Icon(Icons.phone),
                      Expanded(
                        child: TextField(
                          controller: _controller,
                          decoration: InputDecoration.collapsed(
                            hintText: "请输入密码",
                          ),
                          keyboardType: TextInputType.phone,

                        ),
                      ),
                    ],
                  ),
                  Padding(
                    padding: EdgeInsets.symmetric(
                        vertical: ScreenUtil.getInstance().getWidth(4)),
                    child: Divider(
                      height: ScreenUtil.getInstance().getHeight(2),
                      color: Colors.blueGrey,
                    ),
                  ),
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      "*请输入6-20位登录密码",
                      style: TextStyle(
                          fontSize: ScreenUtil.getInstance().getWidth(14)),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: ScreenUtil.getInstance().getHeight(40),
            ),

            Padding(
              padding: EdgeInsets.symmetric(
                  horizontal: ScreenUtil.getInstance().getWidth(34)),
              child: SizedBox(
                height: ScreenUtil.getInstance().getHeight(44),
                width: double.infinity,
                child: RaisedButton(
                  color: Colors.pink,
                  textColor: Colors.white,
                  child: Text('下一步'),
                  onPressed: () {
                    AuntData.requestLogin(widget._phone,_controller.text).then((value){
                      if(value is AuntEntity){
                        SpUtil.putObject(KeyConstant.key_login, LoginInfo(widget._phone,_controller.text));
                        Provider.of<AuntEntity>(context, listen: false).updateAuntInfo(value.userinfo);
                        AuntApp.router.navigateTo(context, Routes.main);
                      }

                    });

                  },
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                  ),
                ),
              ),
            ),//下一步
          ],
        ),
      ),
    );
  }
}

/*-------------------输入手机号------------------------*/
class InputPhoneScreen extends StatefulWidget {
  @override
  _InputPhoneScreenState createState() => _InputPhoneScreenState();
}

class _InputPhoneScreenState extends State<InputPhoneScreen> {
  TextEditingController _controller;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Map loginInfo = SpUtil.getObject(KeyConstant.key_login);

    _controller = new TextEditingController(text:loginInfo!=null?(loginInfo["phone"]??""):"");


  }
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Padding(
              padding: EdgeInsets.symmetric(
                  horizontal: ScreenUtil.getInstance().getWidth(16)),
              child: Container(
                height: ScreenUtil.getInstance().getHeight(50),
                child: Icon(Icons.arrow_back),
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(
                  horizontal: ScreenUtil.getInstance().getWidth(16)),
              child: Text("请输入手机号码"),
            ),
            Padding(
              padding: EdgeInsets.symmetric(
                  horizontal: ScreenUtil.getInstance().getWidth(16)),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text("当前身份：服务人员"),
                  Row(
                    children: <Widget>[
                      Icon(Icons.loop),
                      Text(
                        '切换身份',
                        style: TextStyle(
                          fontSize: 8,
                        ),
                      )
                    ],
                  ),
                ],
              ),
            ),
            SizedBox(
              height: ScreenUtil.getInstance().getHeight(60),
            ),
            Padding(
              padding: EdgeInsets.symmetric(
                  horizontal: ScreenUtil.getInstance().getWidth(36)),
              child: Column(
                children: [
                  Row(
                    children: [
                      Icon(Icons.phone),
                      Expanded(
                        child: TextField(
                          controller: _controller,
                          decoration: InputDecoration.collapsed(
                            hintText: "请输入手机号",
                          ),
                          keyboardType: TextInputType.phone,
                        ),
                      ),
                    ],
                  ),
                  Padding(
                    padding: EdgeInsets.symmetric(
                        vertical: ScreenUtil.getInstance().getWidth(4)),
                    child: Divider(
                      height: ScreenUtil.getInstance().getHeight(2),
                      color: Colors.blueGrey,
                    ),
                  ),
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      "*未注册的手机号验证后自动创建表叔云服账户",
                      style: TextStyle(
                          fontSize: ScreenUtil.getInstance().getWidth(14)),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: ScreenUtil.getInstance().getHeight(40),
            ),
            Padding(
              padding: EdgeInsets.symmetric(
                  horizontal: ScreenUtil.getInstance().getWidth(34)),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Icon(
                    Icons.print,
                    size: 15,
                  ),
                  Text.rich(TextSpan(children: [
                    TextSpan(
                      text: "我已阅读并同意",
                      style: TextStyle(
                          fontSize: ScreenUtil.getInstance().getWidth(13),
                          color: Colors.redAccent),
                    ),
                    TextSpan(
                      text: "《服务人员协议》",
                      style: TextStyle(
                          fontSize: ScreenUtil.getInstance().getWidth(13),
                          color: Colors.blue),
                    ),
                    TextSpan(
                      text: "《隐私政策》",
                      style: TextStyle(
                          fontSize: ScreenUtil.getInstance().getWidth(13),
                          color: Colors.blue),
                    ),
                  ]))
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(
                  horizontal: ScreenUtil.getInstance().getWidth(34)),
              child: SizedBox(
                height: ScreenUtil.getInstance().getHeight(44),
                width: double.infinity,
                child: RaisedButton(
                  color: Colors.pink,
                  textColor: Colors.white,
                  child: Text('下一步'),
                  onPressed: () {
                    AuntApp.router.navigateTo(context, Routes.inputPassword,
                        routeSettings: RouteSettings(arguments: _controller.text));
                  },
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                  ),
                ),
              ),
            ),//下一步
          ],
        ),
      ),
    );
  }
}
